package com.cheejee.goodscenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cheejee.goodscenter.dao.GoodsBrandPropertiesMapper;
import com.cheejee.goodscenter.module.entity.GoodsBrandProperties;
import com.cheejee.goodscenter.service.GoodsBrandPropertiesService;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author CARRY
 * @Date 2020年10月28日上午12:25:01
 */
@Service
public class GoodsBrandPropertiesServiceImpl implements GoodsBrandPropertiesService {

	@Resource
	private GoodsBrandPropertiesMapper gbpm;

	@Override
	public GoodsBrandProperties queryById(Long id) {

		return gbpm.selectByPrimaryKey(id);
	}

	@Override
	public Page<GoodsBrandProperties> queryGoodsBrandProperties(Long goodsId, Integer brandPropertiesTemplateId, String propertiesName, int pageNum, int pageSize) {

		return PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> gbpm.selectBySelective(goodsId, brandPropertiesTemplateId, propertiesName));
	}

	@Override
	@Check(message = "新增的商品品牌属性不能为空", group = InsertGroup.class)
	public int addGoodsBrandProperties(GoodsBrandProperties properties) {

		return gbpm.insertSelective(properties);
	}
	

	@Override
	@Check(message = "新增的商品品牌属性不能为空", group = InsertGroup.class)
	public int addGoodsBrandProperties(List<GoodsBrandProperties> properties) {
		
		return gbpm.insertMany(properties);
	}


	@Override
	public int changeGoodsBrandPropertiesValue(Long propertiesId, String propertiesValue){
		checkExist(propertiesId);
		GoodsBrandProperties properties = new GoodsBrandProperties();
		properties.setId(propertiesId);
		properties.setPropertiesValue(propertiesValue);

		return gbpm.updateByPrimaryKeySelective(properties);
	}

	@Override
	public int deleteGoodsBrandProperties(Long... id) {

		return gbpm.deleteById(id);
	}
	
	private void checkExist(Long id) {
		if(gbpm.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("商品品牌属性不存在");
		}
	}

}
